// pages/my/putphone.js
import Router from '../../utils/router.js'
import base from '../../config/base.js'
import service from '../../services/money.js'
let timer = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    regbtntxt: '获取验证码',
    regflag: 0,
    moneyflag: 1,
    verifyCode: '',
    codeflag: false,
  },
  countdown: function () {
    let that = this,
      regflag = 1,
      cont = 60,
      regbtntxt = '';
    that.setData({
      regflag
    });
    var timergn = function () {
      timer = setTimeout(function () {
        cont--;
        if (cont <= 0) {
          regflag = 0;
          regbtntxt = '重新获取'
          clearTimeout(timer);
          that.setData({
            regflag,
            regbtntxt
          });
        } else {
          jian();
        }
      }, 1000);
    }
    var jian = function () {
      regbtntxt = cont + 's后重新发送';
      that.setData({
        regbtntxt
      })
      timergn();
    }
    let params = {
      phone: that.data.phone
    }
    service.withdrawverifycode(params, res => {
      if (res && res.code == 1) {
        timergn();
      } else {
        that.setData({
          regflag: 2,
          codeflag: true
        });
        wx.showToast({
          title: '验证码发送失败，请稍后再试',
          icon: 'none',
          duration: 5000,
        })
      }
    });

  },
  
  bindKeyInput: function (e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },
  getmoney: function () {
    let that = this,
      params = {
        phone: that.data.phone
      },
      moneyflag = 0,
      verifyCode = that.data.verifyCode;
    that.setData({
      moneyflag
    });
    if (verifyCode === '') {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none',
        complete: function () {
          that.setData({
            moneyflag: 1
          });
        }
      })
      return;
    }
    params.verifyCode = verifyCode;
    service.getwithdraw(params, res => {
      if (res && res.code == 1) {
        return wx.showToast({
          title: '提现成功',
          duration: 5000,
          complete: () => {
            setTimeout(() => {
              Router.go('my');
            }, 1500);
          }
        })
      } else if (res && res.code == 4021) {
        that.toast('验证码错误或已过期，请重新获取！');
      } else if (res && res.code == 4012) {
        that.toast('验证码错误');
      } else if (res && res.code == 4025) {
        that.toast('今日提现次数已达上限');
      } else if (res && res.code == 4023) {
        that.toast('余额不足3元,无法提现');
      } else {
        that.toast('提现失败，请联系客服！');
      }
    }, res => {
      if (res && res.statusCode != 200) {
        that.setData({
          moneyflag: 1
        });
      }
    });
  },
  toast: function (s) {
    wx.showToast({
      title: s,
      icon: 'none',
      duration: 5000
    })
    this.setData({
      moneyflag: 1
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.phone) {
      this.setData({
        phone: options.phone
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分亨
   */
  // onShareAppMessage: function () {

  // }
})